package com.example.hasee.test.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.example.hasee.test.R;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 */
public class fourFragment extends Fragment implements View.OnClickListener {


    private View inflate;
    private Button login_qq;
    private Tencent mTencent;

    public fourFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_four, container, false);
        mTencent = Tencent.createInstance("1106416316", getContext().getApplicationContext());

        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {
        login_qq = (Button) inflate.findViewById(R.id.login_qq);

        login_qq.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_qq:
                BaseUiListener my = new  BaseUiListener ();
                mTencent.login(getActivity(),"all",my);

                break;
        }
    }
    class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object o) {
            Toast.makeText(getContext(), "授权成功", Toast.LENGTH_SHORT).show();
            JSONObject obj = (JSONObject) o;
            try {
                String openID = obj.getString("openid");
                String accessToken = obj.getString("access_token");
                String expires = obj.getString("expires_in");

                mTencent.setOpenId(openID);
                mTencent.setAccessToken(accessToken,expires);
                QQToken qqToken = mTencent.getQQToken();
                UserInfo mUserInfo = new UserInfo(getContext().getApplicationContext(), qqToken);
                mUserInfo.getUserInfo(new IUiListener() {
                    @Override
                    public void onComplete(Object response) {
                        Toast.makeText(getContext(), "登陆成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(UiError uiError) {
                        Toast.makeText(getContext(), "登陆失败", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onCancel() {
                        Toast.makeText(getContext(), "登陆取消", Toast.LENGTH_SHORT).show();

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onError(UiError uiError) {
            Toast.makeText(getContext(), "授权失败", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCancel() {
            Toast.makeText(getContext(), "授权取消", Toast.LENGTH_SHORT).show();

        }
    }
}
